<?php

namespace App\Services;



use App\Models\GoodsOrder;
use App\Models\School;
use App\Models\Supplier;
use App\Models\SupplierOrder;
use App\Models\SupplierSchool;
use TencentCloud\Cpdp\V20190820\Models\Order;

class SupplierService extends BaseService
{
    /**
     * 根据登录名获取登录用户
     * @param $username
     * @return User|null|Model
     */
    public function getByUsresname($username){
        return Supplier::query()->where('username',$username)->where('deleted_at',null)->first();
    }
    /**
     * 根据手机号获取登录用户
     * @param $phone
     * @return User|null|Model
     */
    public function getByPhone($phone){
        return Supplier::query()->where('phone',$phone)->where('deleted_at',null)->first();
    }
    /**
     * 获取列表
     * @return
     */
    public function list($page,$limit,$sort,$order,$category, $pid,$name) {
        $query = Supplier::query();
        $query->select('id','name','register_capital','register_address','production_address',
            'logo','username','phone');
        if(isset($category)){
            $query->where('category',$category);
        }
        if(isset($pid)){
            $query->where('pid',$pid);
        }
        if(!empty($name)){
            $query->where('name','like',"%$name%");
        }
        return $query->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
    }
    /**
     * 获取供应商信息
     * @return
     */
    public function info($id) {
        $supplier = Supplier::where('id',$id)->select('id','name','register_capital','register_address','production_address',
            'logo','description','username','phone','pid','category')->first();
        return $supplier;
    }

    /**
     * 获取未被分配到供应商的学校列表
     * @return
     */
    public function notAllocationSchool($supplier_id) {
        //分配供应商的学校id不包括分配自己的
        $schoolId = SupplierSchool::where('supplier_id','<>',$supplier_id)->get()->pluck('school_id');
        //获取未被分配的学校
        $schoolArr = School::whereNotIn('id',$schoolId)->select('id','name')->get();
        //获取已经分配自己的学校id
        $supplierSchoolId = SupplierSchool::where('supplier_id',$supplier_id)->get()->pluck('school_id');
        //获取已经有订单被修改的学校
        return [
            'schoolArr'=>$schoolArr,
            'supplierSchoolId'=>$supplierSchoolId,
        ];
    }
    /**
     * 查看学校分配的供应商
     * @return
     */
    public function allocationSchoolSupplier($school_id) {

        $supplierSchool = SupplierSchool::where('school_id',$school_id)->first();
        return $supplierSchool;
    }
    /**
     * 分配学校给供应商
     * @return
     */
    public function allocationSchool($supplier_id,$schoolArr) {
        //先删除已有的在添加
        SupplierSchool::where('supplier_id', $supplier_id)->delete();
        foreach ($schoolArr as $v){
            $supplierSchool = new SupplierSchool;
            $supplierSchool->supplier_id = $supplier_id;
            $supplierSchool->school_id  = $v;
            $supplierSchool->save();
        }
        //将学校的订单分配供应商
        $orderArr = GoodsOrder::whereIn('school_id',$schoolArr)->get();

        foreach ($orderArr as $v){
            $arr = $this->getSupplierOrder($supplier_id,$v->order_num);
            if(count($arr) < 1){
                $this->allocationOrder($supplier_id,$v->order_num);
            }
        }

    }
    /**
     * 根据供应商与订单id获取供应商订单
     * @return
     */
    public function getSupplierOrder($supplier_id,$order_num) {
        $orderArr = SupplierOrder::where('supplier_id', $supplier_id)->where('order_num', $order_num)->get();
        return $orderArr;
    }
    /**
     * 供应商分配订单
     * @return
     */
    public function allocationOrder($supplier_id,$order_num,$allocation_type = 0) {
        $supplierOrderModel = new supplierOrder();
        $supplierOrderModel->supplier_id = $supplier_id;
        $supplierOrderModel->order_num = $order_num;
        $supplierOrderModel->allocation_type= $allocation_type;
        $supplierOrderModel->save();
        return $supplierOrderModel->id;
    }

}
